package model;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: a
 * Date: 2022-12-06
 * Time: 22:38
 */
public class HashPromulgatorToReceiver {
    public static UserConnect[] userConnects = new UserConnect[10000007];

    public void insert(int id, User user){
        UserConnect node = userConnects[id];
        if(node == null){
            UserConnect userConnect = new UserConnect(user);
            userConnects[id] = userConnect;
        }else{
            while(node.next != null){
                node = node.next;
            }
            UserConnect userConnect = new UserConnect(user);
            node.next = userConnect;
        }
        System.out.println("订阅成功");
        node = userConnects[id];
        while(node != null){
            System.out.println(node.user.getUserid());
            node = node.next;
        }
    }

    public boolean find(int id, User user){
        UserConnect cur = userConnects[id];
        while(cur != null){
            if(cur.user.getUserid() == user.getUserid()){
                return true;
            }
        }
        return false;
    }

    public void delete(int id, User user){
        if(find(id, user)){
            UserConnect head = new UserConnect();
            UserConnect cur = userConnects[id];
            head.next = cur;
            UserConnect t = head;
            while(cur != null){
                if(cur.user.getUserid() == user.getUserid()) {
                    head.next = cur.next;
                    userConnects[id] = t.next;
                    break;
                }
                head = cur;
                cur = cur.next;
            }
        }else{
            System.out.println("不存在取消订阅情况");
        }
        UserConnect node = userConnects[id];
        if(node == null){
            System.out.println("暂无订阅状态");
        }
        while(node != null){
            System.out.println(node.user.getUserid());
            node = node.next;
        }
    }
}
